//
//  GDMVisitorLoginView.swift
//  SinaWeibo
//
//  Created by mac on 15/11/27.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

//定义访客视图的协议   optional 是OC中关键字
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //协议提供方法
    //必选的协议方法
    //登录的协议方法
    optional func userWilllogin()
    
//    默认这个方法是必须实现的，上面的那个就是一个可选的方法
    //注册的协议方法
    func userWillRegister()
}

class GDMVisitorLoginView: UIView {

    
    //提供代理对象 weak
    //在swift中 所有的属性 默认都是strong
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
//    加载这些子控件
   override init(frame: CGRect) {
    
    super.init(frame: frame)
    
    setUpSubviews()
    
    }

   required init?(coder aDecoder: NSCoder) {
//       fatalError("init(coder:) has not been implemented")
    super.init(coder: aDecoder)
    
   }
    
    func setUpinfo(tipText : String , imageName : String?){
        
        tipLabel.text = tipText
        
        if let name = imageName {
            
//            设置那个圆的那个图片只有在首页显示
            circleView.image = UIImage(named: name)
            
            iconImage.hidden = true
            
            //将圆圈视图 放到最顶层
//            bringSubviewToFront(circleView)
            
        }else{
            
//            创建一个动画，开启一个动画
            starAnimation()
            
        }
    }
    
    func starAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")

        anim.repeatCount = MAXFLOAT
        
//        旋转一圈需要的时间
        anim.duration = 10.0
        
        anim.toValue = 2 * M_PI
        
//        完成动画 视图要是在非活跃状态的时候 移除，要在就不让他移除
        anim.removedOnCompletion = false
        
//        添加动画
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
//    添加子视图
    private func setUpSubviews(){
        
        addSubview(circleView)
        
        addSubview(backView)
        
        addSubview(iconImage)
        
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        
        addSubview(registerBtn)
        
//        使用VFL手动添加约束 必须将frame 设置视图的位置的属性 关闭
        
//        因为每一个都要设置，所以进行遍历一下
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
//        设置每一个控件的自动布局
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
//        设置房子布局
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
        
//        设置label的自动布局
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView" : backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[regBtn]", options:[] , metrics: nil, views: ["maskView": backView,"regBtn": registerBtn]))
        
//        设置后面的背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)

//        添加按钮的点击事件
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
    }
    
//    实现两个方法,点击按钮的时候要进行控制器的跳转，通过代理把需要的参数传递回去
//    private func loginDidClick (){
//        
//        print("\(__FUNCTION__)")
//        
//    }
//    
//    private func registerDidClick(){
//        
//        print("\(__FUNCTION__)")
//        
//    }
    
    
//    MARK点击按钮判断是否实现代理协议
    //响应方法
    @objc private func loginDidClick() {
        //visitorViewDelegate?判断代理是否为空
        //userWilllogin! 必须响应登录方法
        //userWilllogin? 是否响应协方法  responderSelector
        visitorViewDelegate?.userWilllogin?()
    }
    
    @objc private func registerDidClick() {
        visitorViewDelegate?.userWillRegister()
    }
    
    
//  懒加载子控件视图
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var circleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var iconImage : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
//    加载label
    private lazy var tipLabel : UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", color: UIColor.grayColor(), fontSize: 15)
    
//    {
//       
//        let l = UILabel()
//        
////       设置文字
//        l.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
////        设置文字的颜色
//        l.textColor = UIColor.grayColor()
//        
////        设置文字的大小
//        l.font = UIFont.systemFontOfSize(15)
//        
////        设置文字自动换行
//        l.numberOfLines = 0
//        
////        设置文字显示居中
//        l.textAlignment = NSTextAlignment.Center
//        
////设置文字自动适应大小
//        l.sizeToFit()
//        
//        return l
//    }()
    
//    添加按钮
    //登录按钮
//    private lazy var loginBtn: UIButton = UIButton(title: "登录", backName: "common_button_white_disable", color: UIColor.darkGrayColor())
//    (title: "登录", imageName: , color: )
    
//    
//    {
//        let btn = UIButton()
//        
//        btn.setTitle("登录", forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
//        btn.sizeToFit()
//        return btn
//        
//    }()
    
    private lazy var loginBtn: UIButton = UIButton(title: "登录", backName: "common_button_white_disable", color: UIColor.darkGrayColor())
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backName: "common_button_white_disable", color: UIColor.orangeColor())
//    private lazy var registerBtn: UIButton = UIButton(title: "关注", backName: "common_button_white_disable", color: UIColor.orangeColor())
//    (title: "注册", imageName: , color: )
    
    
//    {
//        let btn = UIButton()
//        
//        btn.setTitle("注册", forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
//        btn.sizeToFit()
//        return btn
//    }()

    

}
